﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.ListBoxDemos
{
    public partial class ListBoxDragDrop : Window, IWindow
    {
        DragDropManager dragDropManager = new DragDropManager();
        
        public ListBoxDragDrop()
        {
            InitializeComponent();
            foreach (VIBlend.WPF.Controls.ListBoxItem item in this.ListBoxA.Items)
            {
                dragDropManager.AddDragDropElement(item);
                item.AllowDrop = false;
            }

            ListBoxB.AllowDrop = true;
            dragDropManager.AddDragDropElement(this.ListBoxB);

            dragDropManager.DragDrop += new EventHandler<VIBlend.WPF.Controls.DragEventArgs>(dragDropManager_DragDrop);
            dragDropManager.ItemDragStarting += new EventHandler<DragCancelEventArgs>(dragDropManager_ItemDragStarting);
            dragDropManager.ItemDragging += new EventHandler<DragFeedbackEventArgs>(dragDropManager_ItemDragging);
        }

        void dragDropManager_ItemDragging(object sender, DragFeedbackEventArgs e)
        {
            Border border = InitFeedback(e);
            e.FeedbackContent = border;

            Cursor = Cursors.Hand;
        }

        private static Border InitFeedback(DragFeedbackEventArgs e)
        {
            Border border = new Border();
            border.Background = new SolidColorBrush(Color.FromArgb(255, 230, 230, 230));
            border.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 130, 130, 130));
            border.BorderThickness = new Thickness(1);
            ContentPresenter presenter = new ContentPresenter();
            presenter.ContentTemplate = (e.SourceElement as VIBlend.WPF.Controls.ListBoxItem).ContentTemplate;
            border.Child = presenter;
            return border;
        }

        void dragDropManager_ItemDragStarting(object sender, DragCancelEventArgs e)
        {
            if (e.SourceElement == this.ListBoxB)
                e.Cancel = true;

            e.Data = e.SourceElement.Tag;
        }

        void dragDropManager_DragDrop(object sender, VIBlend.WPF.Controls.DragEventArgs e)
        {
            Cursor = Cursors.Arrow;
            this.ListBoxA.Items.Remove(e.SourceElement as VIBlend.WPF.Controls.ListBoxItem);
            this.ListBoxB.Items.Add(e.SourceElement as VIBlend.WPF.Controls.ListBoxItem);
            this.dragDropManager.RemoveDragDropElement(e.SourceElement);

            double total = 0;

            foreach (VIBlend.WPF.Controls.ListBoxItem item in this.ListBoxB.Items)
            {
                total += double.Parse(item.Tag.ToString());
            }

            this.Total.Text = "Total MSRP as Built " + total.ToString("c0");
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

    
        public void SetTheme()
        {
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        } 
    }
}
